#!/usr/bin/env python
# -*- coding: utf-8 -*-

#-*-
# UbiNote - A Lightweight Note-talking Application
#
# Copyright (C) 2006-2009 Shellex <5h3ll3x@gmail.com>
#
# UbiNote is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# UbiNote is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with UbiNote; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

from dbmap import tag
from dbmgr import dbmgr
class tagmgr:
    @staticmethod
    def add(name):
        c = dbmgr.cursor()
        c.execute('''INSERT INTO tag (name) VALUES(?);''',
            (name,));
        dbmgr.commit();
        return c.lastrowid

    @staticmethod
    def remove( id):
        c = dbmgr.cursor()
        c.execute('''DELETE FROM tag WHERE tag.id=?''', (id,))
        dbmgr.commit()
        pass

    @staticmethod
    def search(title):
        pass

    @staticmethod
    def count():
        return len(dbmgr.cursor().execute('select * from tag').fetchall())


    @staticmethod
    def get_popular(count):
        c = dbmgr.cursor()
        tags = []
        raws = c.execute('SELECT * FROM tag ORDER BY count DESC  limit 0, ?;', (count,)).fetchall()
        for e in raws:
            t = tag(e)
            tags.append(t)
        return tags

    @staticmethod
    def get_all():
        c = dbmgr.cursor()
        tags = []
        raws = c.execute('SELECT * FROM tag').fetchall()
        for e in raws:
            t = tag(e)
            tags.append(t)
        return tags

    @staticmethod
    def get(id):
        c = dbmgr.cursor()
        t = tag(c.execute('select * from tag where tag.id=?', (id,)).fetchone())
        return t;

    @staticmethod
    def get_by_name(name):
        c = dbmgr.cursor()
        t = tag(c.execute('select * from tag where tag.name=?', (name,)).fetchone())
        return t;

    @staticmethod
    def attach(id, block_id):
        c = dbmgr.cursor()
        existed = c.execute('''SELECT tag_id, block_id from tag2block 
            where tag2block.tag_id=? 
                and tag2block.block_id=?''', (id, block_id)).fetchone()
        if not existed:
            c.execute('INSERT INTO tag2block (tag_id, block_id) VALUES(?, ?)', (id, block_id));
        count, = c.execute('''SELECT count from tag where tag.id=?''',
                    (id,)).fetchone()
        c.execute('''UPDATE tag SET count=? WHERE tag.id=?''',
                    (count + 1, id))
        dbmgr.commit()

    @staticmethod
    def detach(id, block_id):
        c = dbmgr.cursor()
        count, = c.execute('''SELECT count from tag where tag.id=?''',
                    (id,)).fetchone()
        c.execute('''UPDATE tag SET count=? WHERE tag.id=?''',
                    (count-1, id))
        c.execute('''DELETE FROM tag2block WHERE tag2block.tag_id=? and tag2block.block_id=?''',
            (id, block_id))
        dbmgr.commit()

    @staticmethod
    def detach_all(block_id):
        c = dbmgr.cursor()
        c.execute('''UPDATE tag SET count=count-1 WHERE tag.id in (
            SELECT tag_id FROM tag2block WHERE tag2block.block_id=?)''',
                    (block_id,))
        c.execute('''DELETE FROM tag2block WHERE tag2block.block_id=?''',
            (block_id,))
        dbmgr.commit()

    @staticmethod 
    def get_from_block(block_id):
        c = dbmgr.cursor()
        raws = c.execute('''SELECT * FROM tag where tag.id in (SELECT tag_id from tag2block WHERE tag2block.block_id=?)''', (block_id,)).fetchall()
        ts = []
        for e in raws:
            ts.append(tag(e))
        return ts




